package com.it.day3;

import java.util.concurrent.TimeUnit;

/**
 * ClassName: DeadLockDemo
 * Description: 死锁
 *
 * @Author: csx
 * @Create: 2025/5/9 - 12:54
 * @version: v1.0
 */
public class DeadLockDemo {
    static DeadLockDemo A = new DeadLockDemo();
    static DeadLockDemo B = new DeadLockDemo();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (A){
                System.out.println(Thread.currentThread().getName() + "\t自己持有A锁，期望获得B锁");
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (B){
                    System.out.println(Thread.currentThread().getName() + "\t持有了B锁");
                }
            }
        },"a").start();
        new Thread(() -> {
            synchronized (B){
                System.out.println(Thread.currentThread().getName() + "\t自己持有B锁，期望获得A锁");
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (A){
                    System.out.println(Thread.currentThread().getName() + "\t持有了A锁");
                }
            }
        },"b").start();
    }
}
